# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# File       : user_info_view.py
# Time       ：2023/8/26 22:56
# Author     ：Y-aong
# version    ：python 3.7
# Description：
    根据token获取用户
"""
from flask import request
from flask_restx import fields

from apis.token import token_ns
from public.api_utils.api_decorator import handle_api_error
from public.api_utils.jwt_utils import JWTUtils
from public.base_response import generate_response, MarshalModel
from public.base_view import AbstractView

user_info = {'token': fields.String()}
payload = {
    'login_type': fields.String(),
    'login_value': fields.String(),
    'password': fields.String(),
}
user_info_marshal = MarshalModel('user_info', token_ns)


class UserTokenView(AbstractView):
    url = '/user_info'

    def __init__(self, *args, **kwargs):
        super(UserTokenView, self).__init__(*args, **kwargs)
        self.token = request.args.get('token')
        self.jwt_utils = JWTUtils()
        self.is_logger = True

    @handle_api_error()
    @token_ns.expect(user_info_marshal.detail_request(user_info))
    @token_ns.marshal_with(user_info_marshal.detail_response(payload, False))
    def get(self):
        """获取用户"""
        return generate_response(self.jwt_utils.verify_token(self.token))
